using System;
using System.Data;
using System.Data.SqlClient;

namespace MessageBoard
{
	/// <summary>
	/// Summary description for Forum.
	/// </summary>
	public class Forum : Connection
	{
		#region Fields
		private int _Id;
		private Category _Category;
		private string _Title;
		private string _Description;
		private int _SortingOrder;
		private Boolean _Active;
		private User _AddedBy;
		private DateTime _AddedOn;
		#endregion

		#region Constructors
		public Forum()
		{
			_Title = "";
			_Description = "";
			_SortingOrder = 1;
			_Active = true;
		}
		#endregion

		#region Functions
		public static Forum Get(System.Data.DataRow dr)
		{
			Forum forum = new Forum();
			forum.Id = int.Parse(dr["Id"].ToString());
			forum.Category = Category.Get(int.Parse(dr["CategoryId"].ToString()));
			forum.Title = dr["Title"].ToString();
			forum.Description = dr["Description"].ToString();
			forum.SortingOrder = int.Parse(dr["SortingOrder"].ToString());
			forum.Active = Convert.ToBoolean(dr["Active"].ToString());
			forum.AddedBy = User.Get(int.Parse(dr["AddedBy"].ToString()));
			forum.AddedOn = Convert.ToDateTime(dr["AddedOn"].ToString());
			
			return forum;
		}

		public static Forum Get(int id)
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT * FROM " + Connection.tblForum + " WHERE [Id] = @Id", DbConnection);
			getData.SelectCommand.Parameters.Add("@Id", id);
			getData.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				return Get(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}

		public static System.Collections.ArrayList GetAll(Category category)
		{
			System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter("SELECT * FROM " + Connection.tblForum + " WHERE [CategoryId] = @CategoryId", DbConnection);
			getData.SelectCommand.Parameters.Add("@CategoryId", category.Id);
			getData.Fill(ds);
			foreach(System.Data.DataRow dr in ds.Tables[0].Rows)
			{
				arrayList.Add(Get(dr));
			}
			return arrayList;
		}
		#endregion
		
		#region Methods
		public void Insert()
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			INSERT INTO " + Connection.tblForum + @" 
			([CategoryId], [Title], [Description], [SortingOrder], [Active], [AddedBy], [AddedOn])
			VALUES
			(@CategoryId, @Title, @Description, @SortingOrder, @Active, @AddedBy, @AddedOn);
			SELECT @@identity as i", DbConnection);
			getData.SelectCommand.Parameters.Add("@CategoryId", Category.Id);
			getData.SelectCommand.Parameters.Add("@Title", Title);
			getData.SelectCommand.Parameters.Add("@Description", Description);
			getData.SelectCommand.Parameters.Add("@SortingOrder", SortingOrder);
			getData.SelectCommand.Parameters.Add("@Active", Active);
			getData.SelectCommand.Parameters.Add("@AddedBy", _AddedBy.Id);
			getData.SelectCommand.Parameters.Add("@AddedOn", _AddedOn);
			
			getData.Fill(ds);
			if(ds.Tables[0].Rows.Count>0)
			{
				Id = int.Parse(ds.Tables[0].Rows[0]["i"].ToString());
			}
		}

		public void Update()
		{
			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			UPDATE " + Connection.tblForum + @" SET 
			[CategoryId] = @CategoryId,
			[Title] = @Title,
			[Description] = @Description,
			[SortingOrder] = @SortingOrder,
			[Active] = @Active,
			[AddedBy] = @AddedBy,
			[AddedOn] = @AddedOn
			WHERE [Id] = @Id", DbConnection);
			getData.SelectCommand.Parameters.Add("@CategoryId", Category.Id);
			getData.SelectCommand.Parameters.Add("@Title", Title);
			getData.SelectCommand.Parameters.Add("@Description", Description);
			getData.SelectCommand.Parameters.Add("@SortingOrder", SortingOrder);
			getData.SelectCommand.Parameters.Add("@Active", Active);
			getData.SelectCommand.Parameters.Add("@AddedBy", _AddedBy.Id);
			getData.SelectCommand.Parameters.Add("@AddedOn", _AddedOn);
			
			getData.SelectCommand.Parameters.Add("@Id", Id);
			getData.Fill(ds);
		}

		public void Delete()
		{
			// Delete the related threads first.
			DeleteThreads();

			DataSet ds = new DataSet();
			SqlDataAdapter getData = new SqlDataAdapter(@"
			DELETE FROM " + Connection.tblForum + @" WHERE [Id] = @Id", DbConnection);
			getData.SelectCommand.Parameters.Add("@Id", Id);
			getData.Fill(ds);
		}

		/// <summary>
		/// Delete the related threads.
		/// </summary>
		private void DeleteThreads()
		{
			foreach(Thread thread in Thread.GetAll(this))
			{
				thread.Delete();
			}
		}
		#endregion

		#region Properties
		
		/// <summary>
		/// The Id of the forum.
		/// </summary>
		public int Id
		{
			get
			{
				return _Id;
			}
			set
			{
				_Id = value;
			}
		}

		/// <summary>
		/// The category of the forum.
		/// </summary>
		public Category Category
		{
			get
			{
				return _Category;
			}
			set
			{
				_Category = value;
			}
		}

		/// <summary>
		/// The Title of the forum.
		/// </summary>
		public string Title
		{
			get
			{
				return _Title;
			}
			set
			{
				_Title = value;
			}
		}

		/// <summary>
		/// the Description of the forum.
		/// </summary>
		public string Description
		{
			get
			{
				return _Description;
			}
			set
			{
				_Description = value;
			}
		}

		/// <summary>
		/// The Sorting Order of the forum.
		/// </summary>
		public int SortingOrder
		{
			get
			{
				return _SortingOrder;
			}
			set
			{
				_SortingOrder = value;
			}
		}

		/// <summary>
		/// If this forum is active or not.
		/// </summary>
		public Boolean Active
		{
			get
			{
				return _Active;
			}
			set
			{
				_Active = value;
			}
		}

		/// <summary>
		/// Get the number of threads in this forum.
		/// </summary>
		public int Threads
		{
			get
			{
				return Thread.Count(this);
			}
		}

		/// <summary>
		/// Get the number of posts in this forum.
		/// </summary>
		public int Posts
		{
			get
			{
				return Post.Count(this);
			}
		}

		/// <summary>
		/// Get the latest thread added in this forum
		/// </summary>
		public Thread LatestThread
		{
			get
			{
				return Thread.GetLatest(this);
			}
		}

		/// <summary>
		/// The user who add this forum.
		/// </summary>
		public User AddedBy
		{
			get
			{
				return _AddedBy;
			}
			set
			{
				_AddedBy = value;
			}
		}

		/// <summary>
		/// The date this forum is added.
		/// </summary>
		public DateTime AddedOn
		{
			get
			{
				return _AddedOn;
			}
			set
			{
				_AddedOn = value;
			}
		}
		#endregion
	}
}
